{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "e39ef2c8-e8c6-429e-87a6-c350fe7035d1",
   "metadata": {
    "ExecutionIndicator": {
     "show": true
    },
    "execution": {
     "iopub.execute_input": "2024-01-15T05:05:51.830327Z",
     "iopub.status.busy": "2024-01-15T05:05:51.829959Z",
     "iopub.status.idle": "2024-01-15T05:06:00.088306Z",
     "shell.execute_reply": "2024-01-15T05:06:00.087722Z",
     "shell.execute_reply.started": "2024-01-15T05:05:51.830303Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting package metadata (repodata.json): | ERROR conda.auxlib.logz:stringify(171): Expecting value: line 1 column 1 (char 0)\n",
      "Traceback (most recent call last):\n",
      "  File \"/opt/conda/lib/python3.10/site-packages/requests/models.py\", line 971, in json\n",
      "    return complexjson.loads(self.text, **kwargs)\n",
      "  File \"/opt/conda/lib/python3.10/site-packages/simplejson/__init__.py\", line 514, in loads\n",
      "    return _default_decoder.decode(s)\n",
      "  File \"/opt/conda/lib/python3.10/site-packages/simplejson/decoder.py\", line 386, in decode\n",
      "    obj, end = self.raw_decode(s)\n",
      "  File \"/opt/conda/lib/python3.10/site-packages/simplejson/decoder.py\", line 416, in raw_decode\n",
      "    return self.scan_once(s, idx=_w(s, idx).end())\n",
      "simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)\n",
      "\n",
      "During handling of the above exception, another exception occurred:\n",
      "\n",
      "Traceback (most recent call last):\n",
      "  File \"/opt/conda/lib/python3.10/site-packages/conda/auxlib/logz.py\", line 165, in stringify\n",
      "    requests_models_Response_builder(builder, obj)\n",
      "  File \"/opt/conda/lib/python3.10/site-packages/conda/auxlib/logz.py\", line 141, in requests_models_Response_builder\n",
      "    resp = response_object.json()\n",
      "  File \"/opt/conda/lib/python3.10/site-packages/requests/models.py\", line 975, in json\n",
      "    raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)\n",
      "requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)\n",
      "ERROR conda.auxlib.logz:stringify(171): Expecting value: line 1 column 1 (char 0)\n",
      "Traceback (most recent call last):\n",
      "  File \"/opt/conda/lib/python3.10/site-packages/requests/models.py\", line 971, in json\n",
      "    return complexjson.loads(self.text, **kwargs)\n",
      "  File \"/opt/conda/lib/python3.10/site-packages/simplejson/__init__.py\", line 514, in loads\n",
      "    return _default_decoder.decode(s)\n",
      "  File \"/opt/conda/lib/python3.10/site-packages/simplejson/decoder.py\", line 386, in decode\n",
      "    obj, end = self.raw_decode(s)\n",
      "  File \"/opt/conda/lib/python3.10/site-packages/simplejson/decoder.py\", line 416, in raw_decode\n",
      "    return self.scan_once(s, idx=_w(s, idx).end())\n",
      "simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)\n",
      "\n",
      "During handling of the above exception, another exception occurred:\n",
      "\n",
      "Traceback (most recent call last):\n",
      "  File \"/opt/conda/lib/python3.10/site-packages/conda/auxlib/logz.py\", line 165, in stringify\n",
      "    requests_models_Response_builder(builder, obj)\n",
      "  File \"/opt/conda/lib/python3.10/site-packages/conda/auxlib/logz.py\", line 141, in requests_models_Response_builder\n",
      "    resp = response_object.json()\n",
      "  File \"/opt/conda/lib/python3.10/site-packages/requests/models.py\", line 975, in json\n",
      "    raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)\n",
      "requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)\n",
      "ERROR conda.auxlib.logz:stringify(171): Expecting value: line 1 column 1 (char 0)\n",
      "Traceback (most recent call last):\n",
      "  File \"/opt/conda/lib/python3.10/site-packages/requests/models.py\", line 971, in json\n",
      "    return complexjson.loads(self.text, **kwargs)\n",
      "  File \"/opt/conda/lib/python3.10/site-packages/simplejson/__init__.py\", line 514, in loads\n",
      "    return _default_decoder.decode(s)\n",
      "  File \"/opt/conda/lib/python3.10/site-packages/simplejson/decoder.py\", line 386, in decode\n",
      "    obj, end = self.raw_decode(s)\n",
      "  File \"/opt/conda/lib/python3.10/site-packages/simplejson/decoder.py\", line 416, in raw_decode\n",
      "    return self.scan_once(s, idx=_w(s, idx).end())\n",
      "simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)\n",
      "\n",
      "During handling of the above exception, another exception occurred:\n",
      "\n",
      "Traceback (most recent call last):\n",
      "  File \"/opt/conda/lib/python3.10/site-packages/conda/auxlib/logz.py\", line 165, in stringify\n",
      "    requests_models_Response_builder(builder, obj)\n",
      "  File \"/opt/conda/lib/python3.10/site-packages/conda/auxlib/logz.py\", line 141, in requests_models_Response_builder\n",
      "    resp = response_object.json()\n",
      "  File \"/opt/conda/lib/python3.10/site-packages/requests/models.py\", line 975, in json\n",
      "    raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)\n",
      "requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)\n",
      "ERROR conda.auxlib.logz:stringify(171): Expecting value: line 1 column 1 (char 0)\n",
      "Traceback (most recent call last):\n",
      "  File \"/opt/conda/lib/python3.10/site-packages/requests/models.py\", line 971, in json\n",
      "    return complexjson.loads(self.text, **kwargs)\n",
      "  File \"/opt/conda/lib/python3.10/site-packages/simplejson/__init__.py\", line 514, in loads\n",
      "    return _default_decoder.decode(s)\n",
      "  File \"/opt/conda/lib/python3.10/site-packages/simplejson/decoder.py\", line 386, in decode\n",
      "    obj, end = self.raw_decode(s)\n",
      "  File \"/opt/conda/lib/python3.10/site-packages/simplejson/decoder.py\", line 416, in raw_decode\n",
      "    return self.scan_once(s, idx=_w(s, idx).end())\n",
      "simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)\n",
      "\n",
      "During handling of the above exception, another exception occurred:\n",
      "\n",
      "Traceback (most recent call last):\n",
      "  File \"/opt/conda/lib/python3.10/site-packages/conda/auxlib/logz.py\", line 165, in stringify\n",
      "    requests_models_Response_builder(builder, obj)\n",
      "  File \"/opt/conda/lib/python3.10/site-packages/conda/auxlib/logz.py\", line 141, in requests_models_Response_builder\n",
      "    resp = response_object.json()\n",
      "  File \"/opt/conda/lib/python3.10/site-packages/requests/models.py\", line 975, in json\n",
      "    raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)\n",
      "requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)\n",
      "ERROR conda.auxlib.logz:stringify(171): Expecting value: line 1 column 1 (char 0)\n",
      "Traceback (most recent call last):\n",
      "  File \"/opt/conda/lib/python3.10/site-packages/requests/models.py\", line 971, in json\n",
      "    return complexjson.loads(self.text, **kwargs)\n",
      "  File \"/opt/conda/lib/python3.10/site-packages/simplejson/__init__.py\", line 514, in loads\n",
      "    return _default_decoder.decode(s)\n",
      "  File \"/opt/conda/lib/python3.10/site-packages/simplejson/decoder.py\", line 386, in decode\n",
      "    obj, end = self.raw_decode(s)\n",
      "  File \"/opt/conda/lib/python3.10/site-packages/simplejson/decoder.py\", line 416, in raw_decode\n",
      "    return self.scan_once(s, idx=_w(s, idx).end())\n",
      "simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)\n",
      "\n",
      "During handling of the above exception, another exception occurred:\n",
      "\n",
      "Traceback (most recent call last):\n",
      "  File \"/opt/conda/lib/python3.10/site-packages/conda/auxlib/logz.py\", line 165, in stringify\n",
      "    requests_models_Response_builder(builder, obj)\n",
      "  File \"/opt/conda/lib/python3.10/site-packages/conda/auxlib/logz.py\", line 141, in requests_models_Response_builder\n",
      "    resp = response_object.json()\n",
      "  File \"/opt/conda/lib/python3.10/site-packages/requests/models.py\", line 975, in json\n",
      "    raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)\n",
      "requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)\n",
      "ERROR conda.auxlib.logz:stringify(171): Expecting value: line 1 column 1 (char 0)\n",
      "Traceback (most recent call last):\n",
      "  File \"/opt/conda/lib/python3.10/site-packages/requests/models.py\", line 971, in json\n",
      "    return complexjson.loads(self.text, **kwargs)\n",
      "  File \"/opt/conda/lib/python3.10/site-packages/simplejson/__init__.py\", line 514, in loads\n",
      "    return _default_decoder.decode(s)\n",
      "  File \"/opt/conda/lib/python3.10/site-packages/simplejson/decoder.py\", line 386, in decode\n",
      "    obj, end = self.raw_decode(s)\n",
      "  File \"/opt/conda/lib/python3.10/site-packages/simplejson/decoder.py\", line 416, in raw_decode\n",
      "    return self.scan_once(s, idx=_w(s, idx).end())\n",
      "simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)\n",
      "\n",
      "During handling of the above exception, another exception occurred:\n",
      "\n",
      "Traceback (most recent call last):\n",
      "  File \"/opt/conda/lib/python3.10/site-packages/conda/auxlib/logz.py\", line 165, in stringify\n",
      "    requests_models_Response_builder(builder, obj)\n",
      "  File \"/opt/conda/lib/python3.10/site-packages/conda/auxlib/logz.py\", line 141, in requests_models_Response_builder\n",
      "    resp = response_object.json()\n",
      "  File \"/opt/conda/lib/python3.10/site-packages/requests/models.py\", line 975, in json\n",
      "    raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)\n",
      "requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)\n",
      "done\n",
      "Solving environment: done\n",
      "\n",
      "Downloading and Extracting Packages:\n",
      "\n",
      "Preparing transaction: done\n",
      "Verifying transaction: done\n",
      "Executing transaction: done\n",
      "#\n",
      "# To activate this environment, use\n",
      "#\n",
      "#     $ conda activate chatglm\n",
      "#\n",
      "# To deactivate an active environment, use\n",
      "#\n",
      "#     $ conda deactivate\n",
      "\n"
     ]
    }
   ],
   "source": [
    "!conda env create -f env.yml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "614dbdee-721f-40d8-9ecc-129ac8d59d41",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-01-15T05:06:07.202950Z",
     "iopub.status.busy": "2024-01-15T05:06:07.202619Z",
     "iopub.status.idle": "2024-01-15T05:06:07.822608Z",
     "shell.execute_reply": "2024-01-15T05:06:07.822069Z",
     "shell.execute_reply.started": "2024-01-15T05:06:07.202929Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "# conda environments:\n",
      "#\n",
      "base                     /opt/conda\n",
      "chatglm                  /opt/conda/envs/chatglm\n",
      "\n"
     ]
    }
   ],
   "source": [
    "!conda env list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "8edee3e7-9a69-44e4-a7b8-cbe397146e97",
   "metadata": {
    "ExecutionIndicator": {
     "show": true
    },
    "execution": {
     "iopub.execute_input": "2024-01-15T05:06:10.425306Z",
     "iopub.status.busy": "2024-01-15T05:06:10.424980Z",
     "iopub.status.idle": "2024-01-15T05:06:10.674394Z",
     "shell.execute_reply": "2024-01-15T05:06:10.673805Z",
     "shell.execute_reply.started": "2024-01-15T05:06:10.425287Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "!activate chatglm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "d328514e-c495-4645-ab6e-b6e49396e855",
   "metadata": {
    "ExecutionIndicator": {
     "show": true
    },
    "execution": {
     "iopub.execute_input": "2024-01-15T05:06:13.032889Z",
     "iopub.status.busy": "2024-01-15T05:06:13.032571Z",
     "iopub.status.idle": "2024-01-15T05:06:13.194508Z",
     "shell.execute_reply": "2024-01-15T05:06:13.194005Z",
     "shell.execute_reply.started": "2024-01-15T05:06:13.032869Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Python 3.10.13\n"
     ]
    }
   ],
   "source": [
    "!python --version"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "0333c2c4-d0b8-401a-9e13-db157a974fc7",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-01-15T05:06:59.884163Z",
     "iopub.status.busy": "2024-01-15T05:06:59.883757Z",
     "iopub.status.idle": "2024-01-15T05:06:59.887359Z",
     "shell.execute_reply": "2024-01-15T05:06:59.886896Z",
     "shell.execute_reply.started": "2024-01-15T05:06:59.884119Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2.1.0+cpu'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import torch\n",
    "torch.__version__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8a5ecf18-5c97-4a98-8f6e-bbcc80e9ccf7",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "!apt-get update && apt-get -y install g++ cmake"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "28f37cb9-85d4-489a-bfb6-e7969b47b7a0",
   "metadata": {
    "ExecutionIndicator": {
     "show": true
    },
    "execution": {
     "iopub.execute_input": "2024-01-15T02:07:52.314400Z",
     "iopub.status.busy": "2024-01-15T02:07:52.314069Z",
     "iopub.status.idle": "2024-01-15T02:07:52.480844Z",
     "shell.execute_reply": "2024-01-15T02:07:52.480341Z",
     "shell.execute_reply.started": "2024-01-15T02:07:52.314379Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "cmake version 3.22.1\n",
      "\n",
      "CMake suite maintained and supported by Kitware (kitware.com/cmake).\n"
     ]
    }
   ],
   "source": [
    "!cmake --version"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "6f7dbb26-f413-4d7e-a87b-a299d511236a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-01-15T05:06:45.258888Z",
     "iopub.status.busy": "2024-01-15T05:06:45.258510Z",
     "iopub.status.idle": "2024-01-15T05:06:45.800554Z",
     "shell.execute_reply": "2024-01-15T05:06:45.799948Z",
     "shell.execute_reply.started": "2024-01-15T05:06:45.258868Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Installed kernelspec chatglm3-demo in /root/.local/share/jupyter/kernels/chatglm3-demo\n"
     ]
    }
   ],
   "source": [
    "!ipython kernel install --name chatglm3-demo --user"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "60fb539d-7cdd-45f1-9896-c11ff4a53d7f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-01-15T02:08:31.522421Z",
     "iopub.status.busy": "2024-01-15T02:08:31.522036Z",
     "iopub.status.idle": "2024-01-15T02:08:31.697497Z",
     "shell.execute_reply": "2024-01-15T02:08:31.696746Z",
     "shell.execute_reply.started": "2024-01-15T02:08:31.522397Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Git LFS initialized.\n"
     ]
    }
   ],
   "source": [
    "!git lfs install"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "09373e97-64ec-4359-9e8f-16d7a93bc0d2",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-01-15T02:36:05.104317Z",
     "iopub.status.busy": "2024-01-15T02:36:05.104001Z",
     "iopub.status.idle": "2024-01-15T02:37:15.051854Z",
     "shell.execute_reply": "2024-01-15T02:37:15.051316Z",
     "shell.execute_reply.started": "2024-01-15T02:36:05.104297Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "正克隆到 'chatglm3-6b'...\n",
      "remote: Enumerating objects: 119, done.\u001b[K\n",
      "remote: Counting objects: 100% (18/18), done.\u001b[K\n",
      "remote: Compressing objects: 100% (18/18), done.\u001b[K\n",
      "remote: Total 119 (delta 3), reused 0 (delta 0), pack-reused 101\u001b[K\n",
      "接收对象中: 100% (119/119), 58.99 KiB | 11.80 MiB/s, 完成.\n",
      "处理 delta 中: 100% (45/45), 完成.\n",
      "过滤内容: 100% (15/15), 23.26 GiB | 344.36 MiB/s, 完成.\n"
     ]
    }
   ],
   "source": [
    "!git clone https://www.modelscope.cn/ZhipuAI/chatglm3-6b.git\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "63f8eb4b-d5cb-4fe2-940f-acbeccb9a2b4",
   "metadata": {
    "ExecutionIndicator": {
     "show": false
    },
    "execution": {
     "iopub.execute_input": "2024-01-15T02:35:01.179498Z",
     "iopub.status.busy": "2024-01-15T02:35:01.179192Z",
     "iopub.status.idle": "2024-01-15T02:35:26.563411Z",
     "shell.execute_reply": "2024-01-15T02:35:26.562855Z",
     "shell.execute_reply.started": "2024-01-15T02:35:01.179475Z"
    },
    "scrolled": true,
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "正克隆到 'chatglm.cpp'...\n",
      "remote: Enumerating objects: 600, done.\u001b[K\n",
      "remote: Counting objects: 100% (600/600), done.\u001b[K\n",
      "remote: Compressing objects: 100% (229/229), done.\u001b[K\n",
      "remote: Total 600 (delta 350), reused 600 (delta 350), pack-reused 0\u001b[K\n",
      "接收对象中: 100% (600/600), 1.44 MiB | 145.00 KiB/s, 完成.\n",
      "处理 delta 中: 100% (350/350), 完成.\n",
      "子模组 'third_party/ggml'（https://github.com/ggerganov/ggml.git）已对路径 'third_party/ggml' 注册\n",
      "子模组 'third_party/pybind11'（https://github.com/pybind/pybind11.git）已对路径 'third_party/pybind11' 注册\n",
      "子模组 'third_party/sentencepiece'（https://github.com/google/sentencepiece.git）已对路径 'third_party/sentencepiece' 注册\n",
      "正克隆到 '/mnt/workspace/chatglm.cpp/third_party/ggml'...\n",
      "remote: Enumerating objects: 5678, done.        \n",
      "remote: Counting objects: 100% (1233/1233), done.        \n",
      "remote: Compressing objects: 100% (299/299), done.        \n",
      "remote: Total 5678 (delta 1024), reused 1062 (delta 899), pack-reused 4445        \n",
      "接收对象中: 100% (5678/5678), 6.63 MiB | 4.17 MiB/s, 完成.\n",
      "处理 delta 中: 100% (3516/3516), 完成.\n",
      "正克隆到 '/mnt/workspace/chatglm.cpp/third_party/pybind11'...\n",
      "remote: Enumerating objects: 27512, done.        \n",
      "remote: Counting objects: 100% (263/263), done.        \n",
      "remote: Compressing objects: 100% (118/118), done.        \n",
      "remote: Total 27512 (delta 148), reused 206 (delta 124), pack-reused 27249        \n",
      "接收对象中: 100% (27512/27512), 10.77 MiB | 6.92 MiB/s, 完成.\n",
      "处理 delta 中: 100% (19335/19335), 完成.\n",
      "正克隆到 '/mnt/workspace/chatglm.cpp/third_party/sentencepiece'...\n",
      "remote: Enumerating objects: 4994, done.        \n",
      "remote: Counting objects: 100% (2026/2026), done.        \n",
      "remote: Compressing objects: 100% (276/276), done.        \n",
      "remote: Total 4994 (delta 1790), reused 1802 (delta 1744), pack-reused 2968        \n",
      "接收对象中: 100% (4994/4994), 26.77 MiB | 8.61 MiB/s, 完成.\n",
      "处理 delta 中: 100% (3443/3443), 完成.\n",
      "子模组路径 'third_party/ggml'：检出 '6549d12f2e3176050040a86334f17c001e170f13'\n",
      "子模组路径 'third_party/pybind11'：检出 '8b03ffa7c06cd9c8a38297b1c8923695d1ff1b07'\n",
      "子模组路径 'third_party/sentencepiece'：检出 '635fe8423a249b6e081aacd290d8aef7476c6a28'\n"
     ]
    }
   ],
   "source": [
    "!git clone --recursive https://gitee.com/wfhe/chatglm.cpp.git"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "918ce34b-6c42-4068-9e7a-cab14feef39f",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "!python chatglm.cpp/chatglm_cpp/convert.py -i chatglm3-6b -t q4_0 -o ./chatglm-ggml.bin"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "ca1c0f26-60ff-4778-82d7-a3be96d9104d",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-01-15T02:42:37.494430Z",
     "iopub.status.busy": "2024-01-15T02:42:37.494109Z",
     "iopub.status.idle": "2024-01-15T02:43:33.190133Z",
     "shell.execute_reply": "2024-01-15T02:43:33.189580Z",
     "shell.execute_reply.started": "2024-01-15T02:42:37.494409Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Looking in indexes: https://mirrors.aliyun.com/pypi/simple\n",
      "Processing ./chatglm.cpp\n",
      "  Installing build dependencies ... \u001b[?25ldone\n",
      "\u001b[?25h  Getting requirements to build wheel ... \u001b[?25ldone\n",
      "\u001b[?25h  Installing backend dependencies ... \u001b[?25ldone\n",
      "\u001b[?25h  Preparing metadata (pyproject.toml) ... \u001b[?25ldone\n",
      "\u001b[?25hBuilding wheels for collected packages: chatglm-cpp\n",
      "  Building wheel for chatglm-cpp (pyproject.toml) ... \u001b[?25ldone\n",
      "\u001b[?25h  Created wheel for chatglm-cpp: filename=chatglm_cpp-0.3.1.dev0-cp310-cp310-linux_x86_64.whl size=824182 sha256=922d865f52b3e2adaf099eaa92f1b83188eacc2ceb9637146b5963156a2a6bad\n",
      "  Stored in directory: /tmp/pip-ephem-wheel-cache-0e02ljkl/wheels/93/43/03/b524400d77fd3099f6751ec9c8a15d309c35d4892ff64e06db\n",
      "Successfully built chatglm-cpp\n",
      "\u001b[33mDEPRECATION: omegaconf 2.0.6 has a non-standard dependency specifier PyYAML>=5.1.*. pip 24.0 will enforce this behaviour change. A possible replacement is to upgrade to a newer version of omegaconf or contact the author to suggest that they release a version with a conforming dependency specifiers. Discussion can be found at https://github.com/pypa/pip/issues/12063\u001b[0m\u001b[33m\n",
      "\u001b[0m\u001b[33mDEPRECATION: pytorch-lightning 1.7.7 has a non-standard dependency specifier torch>=1.9.*. pip 24.0 will enforce this behaviour change. A possible replacement is to upgrade to a newer version of pytorch-lightning or contact the author to suggest that they release a version with a conforming dependency specifiers. Discussion can be found at https://github.com/pypa/pip/issues/12063\u001b[0m\u001b[33m\n",
      "\u001b[0mInstalling collected packages: chatglm-cpp\n",
      "Successfully installed chatglm-cpp-0.3.1.dev0\n",
      "\u001b[33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv\u001b[0m\u001b[33m\n",
      "\u001b[0m\n",
      "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m23.3.1\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m23.3.2\u001b[0m\n",
      "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "!pip install ./chatglm.cpp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a1f627fb-2542-46cc-b0f0-23a2a54562fe",
   "metadata": {
    "ExecutionIndicator": {
     "show": true
    },
    "execution": {
     "iopub.execute_input": "2024-01-15T03:09:05.367782Z",
     "iopub.status.busy": "2024-01-15T03:09:05.367453Z",
     "iopub.status.idle": "2024-01-15T03:09:28.865472Z",
     "shell.execute_reply": "2024-01-15T03:09:28.865032Z",
     "shell.execute_reply.started": "2024-01-15T03:09:05.367762Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "ChatMessage(role=\"assistant\", content=\"当然可以！下面是一个使用Python和matplotlib库画圆的简单示例：\n",
       "\n",
       "```python\n",
       "import matplotlib.pyplot as plt\n",
       "import matplotlib.patches as patches\n",
       "\n",
       "# 创建一个画布\n",
       "fig, ax = plt.subplots()\n",
       "\n",
       "# 创建一个圆形（绿色）并将其添加到画布上\n",
       "circle = patches.Circle((0, 0), 1, fill=False, edgecolor='blue')\n",
       "ax.add_patch(circle)\n",
       "\n",
       "# 设置坐标轴的范围\n",
       "ax.set_xlim(-1, 1)\n",
       "ax.set_ylim(-1, 1)\n",
       "\n",
       "# 显示画布\n",
       "plt.show()\n",
       "```\n",
       "\n",
       "这段代码会创建一个简单的画布，并在其中画一个绿色的圆。当你运行这段代码时，你会看到一个美丽的圆形。\n",
       "\n",
       "如果你想要调整圆的大小或颜色，或者添加更多的图形元素，你可以轻松地对其进行修改。\", tool_calls=[])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import chatglm_cpp\n",
    "pipeline = chatglm_cpp.Pipeline(\"../chatglm-ggml.bin\", dtype=\"q4_0\")\n",
    "pipeline.chat([chatglm_cpp.ChatMessage(role=\"user\", content=\"用python画一个圆\"), \n",
    "               chatglm_cpp.ChatMessage(role=\"system\", content=\"你是一位智能AI助手，你叫ChatGLM，你连接着一台电脑，但请注意不能联网。在使用Python解决任务时，你可以运行代码并得到结果，如果运行结果有错误，你需要尽可能对代码进行改进。你可以处理用户上传到电脑上的文件，文件默认存储路径是/mnt/workspace\")])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "73b194c5-e2cf-421d-97ad-cceaaa58a9a8",
   "metadata": {
    "ExecutionIndicator": {
     "show": true
    },
    "execution": {
     "iopub.execute_input": "2024-01-15T02:47:43.887221Z",
     "iopub.status.busy": "2024-01-15T02:47:43.886897Z",
     "iopub.status.idle": "2024-01-15T02:47:46.921475Z",
     "shell.execute_reply": "2024-01-15T02:47:46.920939Z",
     "shell.execute_reply.started": "2024-01-15T02:47:43.887200Z"
    },
    "scrolled": true,
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Looking in indexes: https://mirrors.aliyun.com/pypi/simple\n",
      "Requirement already satisfied: uvicorn in /opt/conda/lib/python3.10/site-packages (0.25.0)\n",
      "Requirement already satisfied: fastapi in /opt/conda/lib/python3.10/site-packages (0.109.0)\n",
      "Requirement already satisfied: pydantic_settings in /opt/conda/lib/python3.10/site-packages (2.1.0)\n",
      "Collecting sse_starlette\n",
      "  Downloading https://mirrors.aliyun.com/pypi/packages/34/7e/d6087916bf58a4343459b47807a116a3a755e6ddd4857f375547e00f6252/sse_starlette-1.8.2-py3-none-any.whl (8.9 kB)\n",
      "Requirement already satisfied: click>=7.0 in /opt/conda/lib/python3.10/site-packages (from uvicorn) (8.1.7)\n",
      "Requirement already satisfied: h11>=0.8 in /opt/conda/lib/python3.10/site-packages (from uvicorn) (0.14.0)\n",
      "Requirement already satisfied: typing-extensions>=4.0 in /opt/conda/lib/python3.10/site-packages (from uvicorn) (4.8.0)\n",
      "Requirement already satisfied: pydantic!=1.8,!=1.8.1,!=2.0.0,!=2.0.1,!=2.1.0,<3.0.0,>=1.7.4 in /opt/conda/lib/python3.10/site-packages (from fastapi) (2.5.2)\n",
      "Requirement already satisfied: starlette<0.36.0,>=0.35.0 in /opt/conda/lib/python3.10/site-packages (from fastapi) (0.35.1)\n",
      "Requirement already satisfied: python-dotenv>=0.21.0 in /opt/conda/lib/python3.10/site-packages (from pydantic_settings) (1.0.0)\n",
      "Requirement already satisfied: anyio in /opt/conda/lib/python3.10/site-packages (from sse_starlette) (4.2.0)\n",
      "Requirement already satisfied: annotated-types>=0.4.0 in /opt/conda/lib/python3.10/site-packages (from pydantic!=1.8,!=1.8.1,!=2.0.0,!=2.0.1,!=2.1.0,<3.0.0,>=1.7.4->fastapi) (0.6.0)\n",
      "Requirement already satisfied: pydantic-core==2.14.5 in /opt/conda/lib/python3.10/site-packages (from pydantic!=1.8,!=1.8.1,!=2.0.0,!=2.0.1,!=2.1.0,<3.0.0,>=1.7.4->fastapi) (2.14.5)\n",
      "Requirement already satisfied: idna>=2.8 in /opt/conda/lib/python3.10/site-packages (from anyio->sse_starlette) (3.4)\n",
      "Requirement already satisfied: sniffio>=1.1 in /opt/conda/lib/python3.10/site-packages (from anyio->sse_starlette) (1.3.0)\n",
      "Requirement already satisfied: exceptiongroup>=1.0.2 in /opt/conda/lib/python3.10/site-packages (from anyio->sse_starlette) (1.2.0)\n",
      "\u001b[33mDEPRECATION: omegaconf 2.0.6 has a non-standard dependency specifier PyYAML>=5.1.*. pip 24.0 will enforce this behaviour change. A possible replacement is to upgrade to a newer version of omegaconf or contact the author to suggest that they release a version with a conforming dependency specifiers. Discussion can be found at https://github.com/pypa/pip/issues/12063\u001b[0m\u001b[33m\n",
      "\u001b[0m\u001b[33mDEPRECATION: pytorch-lightning 1.7.7 has a non-standard dependency specifier torch>=1.9.*. pip 24.0 will enforce this behaviour change. A possible replacement is to upgrade to a newer version of pytorch-lightning or contact the author to suggest that they release a version with a conforming dependency specifiers. Discussion can be found at https://github.com/pypa/pip/issues/12063\u001b[0m\u001b[33m\n",
      "\u001b[0mInstalling collected packages: sse_starlette\n",
      "Successfully installed sse_starlette-1.8.2\n",
      "\u001b[33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv\u001b[0m\u001b[33m\n",
      "\u001b[0m\n",
      "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m23.3.1\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m23.3.2\u001b[0m\n",
      "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "!pip install uvicorn fastapi pydantic_settings sse_starlette"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "ac002937-4186-44e1-ae11-50002fc24f2c",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-01-15T02:50:01.950638Z",
     "iopub.status.busy": "2024-01-15T02:50:01.950336Z",
     "iopub.status.idle": "2024-01-15T02:56:51.063990Z",
     "shell.execute_reply": "2024-01-15T02:56:51.063249Z",
     "shell.execute_reply.started": "2024-01-15T02:50:01.950619Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[32mINFO\u001b[0m:     Started server process [\u001b[36m3259\u001b[0m]\n",
      "\u001b[32mINFO\u001b[0m:     Waiting for application startup.\n",
      "\u001b[32mINFO\u001b[0m:     Application startup complete.\n",
      "\u001b[32mINFO\u001b[0m:     Uvicorn running on \u001b[1mhttp://127.0.0.1:8000\u001b[0m (Press CTRL+C to quit)\n",
      "2024-01-15 10:56:17,418 - openai_api - INFO - prompt: \"Hello! What is your name?\", sync response: \"Hello! I am an AI assistant named ChatGLM3-6B, which is developed based on the language model jointly trained by Tsinghua University KEG Lab and Zhipu AI Company in 2023. My job is to provide appropriate answers and support to users' questions and requests.\"\n",
      "\u001b[32mINFO\u001b[0m:     127.0.0.1:54480 - \"\u001b[1mPOST /v1/chat/completions HTTP/1.1\u001b[0m\" \u001b[32m200 OK\u001b[0m\n",
      "^C\n",
      "\u001b[32mINFO\u001b[0m:     Shutting down\n",
      "\u001b[32mINFO\u001b[0m:     Waiting for application shutdown.\n",
      "\u001b[32mINFO\u001b[0m:     Application shutdown complete.\n",
      "\u001b[32mINFO\u001b[0m:     Finished server process [\u001b[36m3259\u001b[0m]\n"
     ]
    }
   ],
   "source": [
    "!uvicorn openai_api:app --host 127.0.0.1 --port 8000"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "220dcf8a-5535-402c-ad2a-829003f20d2f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-01-15T03:22:41.465268Z",
     "iopub.status.busy": "2024-01-15T03:22:41.464964Z",
     "iopub.status.idle": "2024-01-15T03:22:50.865159Z",
     "shell.execute_reply": "2024-01-15T03:22:50.864529Z",
     "shell.execute_reply.started": "2024-01-15T03:22:41.465245Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "!conda env export > env.yml"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "af3dc59b-b81b-4a91-ba6c-11a7f8f39666",
   "metadata": {
    "ExecutionIndicator": {
     "show": false
    },
    "execution": {
     "iopub.execute_input": "2024-01-15T03:12:21.678113Z",
     "iopub.status.busy": "2024-01-15T03:12:21.677779Z",
     "iopub.status.idle": "2024-01-15T03:12:21.848312Z",
     "shell.execute_reply": "2024-01-15T03:12:21.847820Z",
     "shell.execute_reply.started": "2024-01-15T03:12:21.678092Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[master 8104bb5] add chat_cpp example\n",
      " 1 file changed, 62 insertions(+), 222 deletions(-)\n"
     ]
    }
   ],
   "source": [
    "!git add .\n",
    "!git commit -m 'add chat_cpp example'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "115a4d4b-a928-43bc-8829-41a3cc1231e5",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-01-15T03:12:42.125824Z",
     "iopub.status.busy": "2024-01-15T03:12:42.125480Z",
     "iopub.status.idle": "2024-01-15T03:12:43.903366Z",
     "shell.execute_reply": "2024-01-15T03:12:43.902816Z",
     "shell.execute_reply.started": "2024-01-15T03:12:42.125802Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "枚举对象中: 5, 完成.\n",
      "对象计数中: 100% (5/5), 完成.\n",
      "使用 64 个线程进行压缩\n",
      "压缩对象中: 100% (3/3), 完成.\n",
      "写入对象中: 100% (3/3), 1.35 KiB | 1.35 MiB/s, 完成.\n",
      "总共 3（差异 2），复用 0（差异 0），包复用 0\n",
      "remote: Powered by \u001b[01;33mGITEE.COM \u001b[0m[\u001b[01;35mGNK-6.4\u001b[0m]\u001b[0m\u001b[K\n",
      "To https://gitee.com/wfhe/my_chatglm.git\n",
      "   0f62e98..8104bb5  master -> master\n"
     ]
    }
   ],
   "source": [
    "!git push origin master"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1f5ca2b4-e7b7-4904-bc59-406ae80644d0",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "chatglm3-demo",
   "language": "python",
   "name": "chatglm3-demo"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
